//
//  ViewController.m
//  jiuquan
//
//  Created by geng on 14-11-17.
//  Copyright (c) 2014年 geng. All rights reserved.
//
#import "OAIndexViewController.h"
#import "OAHomeViewController.h"
#import "OAScheduleViewController.h"
#import "OASettingViewController.h"
#import "OAabookViewController.h"
@interface OAIndexViewController ()

@end

@implementation OAIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate=self;
    [self initControllers];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)initControllers
{
    if (_homeView==nil) {
        _homeView=[[OAHomeViewController alloc]init];
        _schedulView=[[OAScheduleViewController alloc]init];
        _abookView=[[OAabookViewController alloc]init];
        _settingView=[[OASettingViewController alloc]init];
    }
    NSArray*controllers=@[_homeView,_schedulView,_abookView,_settingView];
    NSMutableArray*navcontrollers=[[NSMutableArray alloc]init];
    for (UIViewController*view in controllers)
    {
        UINavigationController*navigationView=[[UINavigationController alloc]initWithRootViewController:view];
        navigationView.navigationBar.hidden=YES;
        [navcontrollers addObject:navigationView];
    }
    
    self.viewControllers=navcontrollers;
    _homeView.navigationController.tabBarItem.tag=0;
    _schedulView.navigationController.tabBarItem.tag=1;
    
    _abookView.navigationController.tabBarItem.tag=2;
    
    _settingView.navigationController.tabBarItem.tag=3;
    
    
    [self addCustomTabbarImage];
}
#pragma mark Tabbar Custom UI
- (void)addCustomTabbarImage
{
    if (!IOS7) {
        UIImage*images=[UIImage new];
        self.tabBar.selectionIndicatorImage = images;
    }
    [self.tabBar setBackgroundImage:[UIImage imagefileNamed:@"Common/tab_bg"]];
    if (_item1==nil) {
        _item1 = [[OAIndexItem alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH /4, self.tabBar.frame.size.height)];
    }
    _item1.type=1;
    _item1.normalImage = [UIImage imagefileNamed:@"Common/商家1"];
    _item1.selectedImage = [UIImage imagefileNamed:@"Common/商家2"];
    _item1.title.text=@"总揽";
    [_item1 setSelected:YES];
    [self.tabBar addSubview:_item1];
    
    if (_item2==nil) {
        _item2 = [[OAIndexItem alloc] initWithFrame:CGRectMake(_item1.right, 0, SCREEN_WIDTH / 4, self.tabBar.frame.size.height)];
    }
    _item2.normalImage = [UIImage imagefileNamed:@"Common/历史1"];
    _item2.selectedImage = [UIImage imagefileNamed:@"Common/历史2"];
    _item2.title.text=@"日程";
    _item2.type=1;
    [_item2 setSelected:NO];
    [self.tabBar addSubview:_item2];
    
    
    if (_item3==nil) {
        _item3 = [[OAIndexItem alloc] initWithFrame:CGRectMake(_item2.right, 0, SCREEN_WIDTH / 4, self.tabBar.frame.size.height)];
    }
    _item3.type=1;
    _item3.normalImage = [UIImage imagefileNamed:@"Common/我的1"];
    _item3.selectedImage = [UIImage imagefileNamed:@"Common/我的2"];
    _item3.title.text=@"通讯录";
    [_item3 setSelected:NO];
    [self.tabBar addSubview:_item3];
    
    if (_item4==nil) {
        _item4 = [[OAIndexItem alloc] initWithFrame:CGRectMake(_item3.right, 0, SCREEN_WIDTH / 4, self.tabBar.frame.size.height)];
    }
    _item4.type=1;
    _item4.normalImage = [UIImage imagefileNamed:@"Common/更多1"];
    _item4.selectedImage = [UIImage imagefileNamed:@"Common/更多2"];
    _item4.title.text=@"设置";
    [_item4 setSelected:NO];
    [self.tabBar addSubview:_item4];
    
    
}



//#pragma mark - UItabbarViewDelegate
//-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
//{
//    if (viewController.tabBarItem.tag==3) {
//        if ([GolbalManager sharedManager].isLogin) {
//
//            return YES;
//        }
//        else
//        {
//            LoginViewController*login=[[LoginViewController alloc]init];
//            login.selectIndex=3;
//            UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:login];
//            nav.navigationBar.hidden=YES;
//            [self presentViewController:nav animated:YES completion:nil];
//            return NO;
//        }
//    }
//    return YES;
//}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [self selectedOrnomal];
}
-(void)selectedOrnomal
{
    if (self.selectedIndex==0) {
        [_item1 setSelected:YES];
        [_item2 setSelected:NO];
        [_item3 setSelected:NO];
        [_item4 setSelected:NO];
    }
    else if(self.selectedIndex==1)
    {
        [_item1 setSelected:NO];
        [_item2 setSelected:YES];
        [_item3 setSelected:NO];
        [_item4 setSelected:NO];
    }
    else if(self.selectedIndex==2)
    {
        [_item1 setSelected:NO];
        [_item2 setSelected:NO];
        [_item3 setSelected:YES];
        [_item4 setSelected:NO];
    }
    else
    {
        [_item1 setSelected:NO];
        [_item2 setSelected:NO];
        [_item3 setSelected:NO];
        [_item4 setSelected:YES];
    }
    
}

-(void)makeSelectedIndex:(NSUInteger)selectedIndex
{
    _homeView.navigationController.tabBarItem.tag=0;
    _schedulView.navigationController.tabBarItem.tag=1;
    _abookView.navigationController.tabBarItem.tag=2;
    _settingView.navigationController.tabBarItem.tag=3;
    
    
    self.selectedIndex=selectedIndex;
    [self selectedOrnomal];
}




#pragma mark - UItabbarViewDelegate
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    
    
    return YES;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
 
}



@end
